package com.sidemenu.demo

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.sidemenu.demo.databinding.ActivityMainBinding
import kotlin.collections.remove
import kotlin.text.replace
/**
 *  Created by zxn on 2025/8/29.
 */
class MainActivity : AppCompatActivity() {

    private lateinit var mMainBinding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        setContentView(R.layout.activity_main)
        mMainBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mMainBinding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        mMainBinding.materialSwitch.isChecked = false
        mMainBinding.materialSwitch.setOnCheckedChangeListener { _, isChecked ->
            val text = if (isChecked) "挂载" else "移除"
            Toast.makeText(this, (text), Toast.LENGTH_SHORT).show()
            onReplaceGroupInfoFrag(isChecked)
        }
        //onReplaceChatGroupMenuFrag()
        //ChatGroupBoardActivity.jumpTo(this)
        //onReplaceGroupInfoFrag(true)
    }

    private fun onReplaceChatGroupMenuFrag() {
        supportFragmentManager.beginTransaction()
            .replace(R.id.fcvLayout, ChatGroupMenuFrag.newInstance())
            .commitAllowingStateLoss()
    }

    private fun onReplaceGroupInfoFrag(show: Boolean) {
        //onReplaceChatGroupInfoFrag(show)
        //onReplaceChatGroupMenuFrag(show)
        if (show) {
            supportFragmentManager.beginTransaction()
                .replace(
                    R.id.flInfoContainer,
                    ChatGroupInfoFrag.newInstance(),
                    "ChatGroupInfoFrag"
                )
                .replace(
                    R.id.fcvLayout,
                    ChatGroupMenuFrag.newInstance(),
                    "ChatGroupMenuFrag"
                )
                .commitAllowingStateLoss()
        } else {
            val mInfoFrag =
                supportFragmentManager.findFragmentByTag("ChatGroupInfoFrag")
            val mMenuFrag =
                supportFragmentManager.findFragmentByTag("ChatGroupMenuFrag")
            val beginTransaction = supportFragmentManager.beginTransaction()
            beginTransaction.apply {
                mInfoFrag?.let {
                    beginTransaction.remove(it)

                }
                mMenuFrag?.let {
                    beginTransaction.remove(it)
                }
                commitAllowingStateLoss()
            }

        }
    }
}